% $Id: e_fft.m 332 2005-05-11 22:08:46Z odellj $
% e_fft.m - average eros data spectra across blocks
% Ed Maclin 4/11/02
% Kludged SS (session label) 4/22/03
% $Log: e_fft.m,v $
% Revision 1.11  2004/08/25 22:12:50  odellj
% Added the ability to pass the id_tag into the id_tags structure if this routine is called.
%
% Revision 1.10  2004/07/13 22:23:18  odellj
% Added the new variable _id to the subprogram to output to the inidiviually created files to identify the version of the producing function.
%
% Revision 1.9  2004/07/12 20:37:57  edmaclin
% *** empty log message ***
%
% Revision 1.8  2004/07/12 20:02:27  edmaclin
% *** empty log message ***
%
% Revision 1.7  2004/07/12 18:16:22  odellj
% Typo.
%
% Revision 1.6  2004/07/12 18:14:58  odellj
% Updated the logging of the subroutines.  Added log_file as a parameter for each of their calls.
%
% Revision 1.5  2004/07/12 16:43:33  edmaclin
% computes fft for each block (not average across blocks) > file_name_fft
%
% Revision 1.4  2004/07/09 18:36:54  edmaclin
% changed "out_file_flg" to "out_file_fid"
%
% Revision 1.3  2004/07/07 18:08:05  odellj
% Prepped for logging.
%
% Revision 1.2  2004/06/30 18:13:57  odellj
% Added more verbose fprintf statements.
%
% Revision 1.1.1.1  2004/02/18 20:09:49  odellj
% initial import into CVS
%
% Revision 1.2  2004/02/05 20:45:40  odellj
% Added Id and Log tags.
%

function e_fft(log_file)
global hdr file_name i_block fft_freqs dc_fft ac_fft ph_fft logid id_tags

e_fft_id = '$Id: e_fft.m 332 2005-05-11 22:08:46Z odellj $';
check = strcmp(e_fft_id,id_tags); % Make sure we don't already have the tag
if sum(check) == 0
    id_tags{size(id_tags,1)+1,1} = e_fft_id;  % Add it in then.
end



in_path=[hdr.data_path hdr.exp_name hdr.data_type hdr.hi '-' hdr.lo '\'];
out_path=[hdr.data_path hdr.exp_name hdr.data_type hdr.hi '-' hdr.lo '\'];

n_fft=256;

% Determine whether input and output files exist
%  !!! 'exist' function doesn't work in compiler :-(
%
in_file_fid=fopen([in_path file_name]);
k=findstr('.',file_name);
out_file_name=[file_name(1:k(1)) 'fft'];
out_file_fid=fopen([out_path out_file_name]);
fclose all;

% Open the log file if it exists
%
if ~strcmp(log_file,'')
    logid = fopen(log_file,'a');
else logid = 1; end

if (out_file_fid < 0) & (in_file_fid > 0)
        
		% load EROS data in .mat format
		%
		block=sprintf('%03.0f', i_block);
		fprintf(logid,'Reading %s%s\n',in_path,file_name);
		load([in_path file_name],'dc','ac','ph','mrdc','mrac');
		
		% Compute the spectrum
		%
		for i_ch=1:hdr.n_chans
            tmp=dc(:,i_ch);
            [my_fft fft_freqs]=pwelch(tmp,[],[],[2*n_fft],50);
            dc_fft(:,i_ch)=my_fft;
            tmp=ac(:,i_ch); 
            [my_fft fft_freqs]=pwelch(tmp,[],[],[2*n_fft],50);                        
            ac_fft(:,i_ch)=my_fft;
            tmp=ph(:,i_ch);  
            [my_fft fft_freqs]=pwelch(tmp,[],[],[2*n_fft],50);                        
            ph_fft(:,i_ch)=my_fft;
        end % chan 
        
        % Write out "averages" in .mat format
        %
        out_file_name=[file_name(1:k(1)-1) '_fft.' block];
        msg=sprintf('Writing %s%s\n',out_path,out_file_name);
        fprintf(logid,'Writing %s%s\n',out_path,out_file_name);
        save([out_path out_file_name],'dc_fft','ac_fft','ph_fft','fft_freqs','id_tags')
        
else % if various files exist or don't...
    if (in_file_fid < 0)
        msg=sprintf('%s does not exist.\n',[in_path file_name]);
        fprintf(logid,'%s does not exist.\n',[in_path file_name]);
    end
    if (out_file_fid > 0)
        msg=sprintf('%s already exists!\n',[out_path out_file_name]);
        fprintf(logid,'%s already exists!\n',[out_path out_file_name]);
    end
end

if logid ~= 1, fclose(logid); end